/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.yutel.wuzi.action;

import com.yutel.wuzi.entities.Chess;
import java.util.Vector;

/**
 *
 * @author yuanyajun
 */
public class Calculate {
    public boolean isWin(Vector step){
        //水平
        int size=calcLength(step,-1,0);
        if(size>=4){
            return true;
        }
        size+=calcLength(step,1,0);
        if(size>=4){
            return true;
        }
        //90度
        size=calcLength(step,0,-1);
        if(size>=4){
            return true;
        }
        size+=calcLength(step,0,1);
        if(size>=4){
            return true;
        }
        //45度
        size=calcLength(step,1,-1);
        if(size>=4){
            return true;
        }
        size+=calcLength(step,-1,1);
        if(size>=4){
            return true;
        }
        //135度
        size=calcLength(step,-1,-1);
        if(size>=4){
            return true;
        }
        size+=calcLength(step,1,1);
        if(size>=4){
            return true;
        }
        return false;
    }

    private int calcLength(Vector step,int dx,int dy){
        int length=0;
        Chess last=(Chess)step.lastElement();
        Chess temp=new Chess(last.getX(),last.getY());
        for(int i=0;i<4;i++) {
            temp.setX(temp.getX()+dx);
            temp.setY(temp.getY()+dy);
            int index=step.lastIndexOf(temp);
            if(index>=0 && ((Chess)step.elementAt(index)).isBlack()==last.isBlack()) {
                    length++;
            } else {
                break;
            }
        }
        return length;
    }

}
